package com.example.testproject.adapter

import android.graphics.Color
import android.util.Log
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.example.testproject.R
import com.example.testproject.bean.TopicSecondBean
import com.example.testproject.ext.addOnTextChangedClick
import com.example.testproject.utils.click
import kotlinx.android.synthetic.main.topic_content_second.view.*

/**
 *
 * @ClassName: TopicContentAdapter
 * @Author: android 超级兵
 * @CreateDate: 10/27/21$ 7:48 PM$
 * TODO
 */
class TopicContentAdapter(
    data: List<TopicSecondBean.ContentBean>,
    val type: Int// 类型 [0单选/1多选/2判断/3填空]
) :
    BaseMultiItemQuickAdapter<TopicSecondBean.ContentBean, BaseViewHolder>(data) {

    companion object {
        const val TEXT = 0
        const val FILL = 1
    }

    init {
        addItemType(TEXT, R.layout.topic_content_first)
        addItemType(FILL, R.layout.topic_content_second)
    }

    override fun convert(helper: BaseViewHolder, item: TopicSecondBean.ContentBean) {
        when (helper.itemViewType) {
            TEXT -> {
                helper
                    .setText(
                        R.id.tvFirst, String.format(
                            "(%s).%s", (helper.adapterPosition.toChar() + 65).toString(),
                            item.ContentTitle
                        )
                    )

                // 点击选中改变颜色
                helper.setTextColor(
                    R.id.tvFirst,
                    if (item.isSelect) Color.parseColor("#ff0000") else Color.parseColor("#333333")
                )

                // item点击事件[单选/多选/判断]
                helper.itemView.click {

                    // 单选和判断选中回调
                    if (type == 0 || type == 2) {
                        // 改变选中所有状态为false
                        click.singleChoiceClick(type)
                    }

                    // 反选
                    item.isSelect = !item.isSelect
                    notifyDataSetChanged()

                    // 单选/多选/判断回调
                    click.choiceClick(type)

                }
                Log.i("szjContentTitle1", item.ContentTitle)
            }
            FILL -> {
                helper.itemView.editSecond.hint = item.ContentTitle

                helper.itemView.editSecond.addOnTextChangedClick {
                    item.ContentTitle = it

                    // 如果当前输入内容 >0 则表示"选中"
                    item.isSelect = it.isNotEmpty()
                    Log.i("szjContentTitle2", item.ContentTitle)
                    // 填空回调
                    click.fillClick(type)
                }
            }
        }
    }


    interface OnTopicContentListener {
        /*
         * 作者:android 超级兵
         * 创建时间: 10/28/21 9:36 AM
         * TODO 单选/判断回调
         */
        fun singleChoiceClick(type: Int)

        /*
         * 作者:android 超级兵
         * 创建时间: 10/28/21 9:47 AM
         * TODO 单选/多选/判断点击回调
         */
        fun choiceClick(type: Int)

        /*
         * 作者:android 超级兵
         * 创建时间: 10/28/21 9:48 AM
         * TODO 填空回调
         */
        fun fillClick(type: Int)
    }

    lateinit var click: OnTopicContentListener
}